package com.wjjun.basemvpvm.data.net;

import com.blankj.utilcode.util.DeviceUtils;
import com.google.gson.GsonBuilder;
import com.ihsanbal.logging.Level;
import com.ihsanbal.logging.LoggingInterceptor;
import com.wjjun.basemvpvm.BuildConfig;
import com.wjjun.basemvpvm.data.net.converter.GsonConverterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.internal.platform.Platform;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * http/https接口管理
 * author Wjjun
 * Created 2018/3/19.
 */

public class HttpApiManager {


    public static final String SCHEME = "http";

    //正式
    public static String HOST = "";

    public static int PORT = 80;

    public static String PATH = "";

    private volatile static HttpApiManager instance;

    private OkHttpClient okHttpClient;

    private HttpUrl httpUrl;

    private GsonBuilder gsonBuilder;

    private HttpApiManager() {
        okHttpClient = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor.Builder()
                .loggable(BuildConfig.DEBUG)
                .setLevel(Level.BASIC)
                .log(Platform.INFO)
                .request("Request")
                .response("Response")
                .addHeader("DeviceType", "Android")
                .addHeader("SoftVer", String.valueOf(BuildConfig.VERSION_CODE)) //app版本号，1
                .addHeader("AndroidID", DeviceUtils.getAndroidID())
                .addHeader("SystemVer", DeviceUtils.getSDKVersionName())   //安卓版本, 8.0.0
                .addHeader("Manufacturer", DeviceUtils.getManufacturer())   //厂家，Xiaomi
                .addHeader("Model", DeviceUtils.getModel()) //型号， MIX2
                .addHeader("AppId", "10001")    // 10001 清米农户版本 10002 清米合作社版本
                .build())
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();

        gsonBuilder = new GsonBuilder().disableHtmlEscaping();
        gsonBuilder.registerTypeAdapter(String.class, new StringConverter());
    }

    public static HttpApiManager getInstance() {
        if (instance == null) {
            synchronized (HttpApiManager.class) {
                if (instance == null) {
                    instance = new HttpApiManager();
                }
            }
        }
        return instance;
    }

    public HttpApiManager init(String host, int port, String path) {
        HOST = host;
        PORT = port;
        PATH = path;
        httpUrl = new HttpUrl.Builder().scheme(SCHEME).host(HOST).port(PORT).addEncodedPathSegments(PATH).build();
        return this;
    }

    private Retrofit createNewRetrofit(HttpUrl httpUrl, OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .baseUrl(httpUrl)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gsonBuilder.create()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
                .build();
    }

    public <T> T getService(Class<T> clazz) {
        Retrofit retrofit = createNewRetrofit(httpUrl, okHttpClient);
        return retrofit.create(clazz);
    }

}
